package com.wish.wishsystem_management.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wish.wishsystem_management.api.SysLog;
import com.wish.wishsystem_management.entity.SysRole;
import com.wish.wishsystem_management.entity.SysUser;
import com.wish.wishsystem_management.entity.TbBusiness;
import com.wish.wishsystem_management.service.*;
import dto.systemDto.SysUserDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import vo.ResultEntity;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 系统用户 前端控制器
 * </p>
 *
 * @author zhaiwj
 * @since 2021-03-22
 */
@RestController
@RequestMapping("/sysUser")
@Slf4j
public class SysUserController {

    @Autowired
    private ISysUserService iSysUserService;

    @Autowired
    private ISysDeptService iSysDeptService;

    @Autowired
    private ISysRegionService iSysRegionService;

    @Autowired
    private ISysUserRoleService iSysUserRoleService;

    @Autowired
    private ISysRoleDeptService iSysRoleDeptService;

    @Autowired
    private ITbConfigUserBusinessCircleService iTbConfigUserBusinessCircleService;

    @Autowired
    ITbBusinessCircleService iTbBusinessCircleService;

    @RequestMapping("/list")
    public ResultEntity list(SysUserDto sysUserDto) {
        PageHelper.startPage(sysUserDto.getPageNum(), sysUserDto.getPageSize());
        List<SysUser> list = iSysUserService.selectPageVo(sysUserDto);
        PageInfo<SysUser> info = new PageInfo<>(list);
        return ResultEntity.ok("200", "列表数据", info);
    }



    @SysLog("保存用户")
    @RequestMapping("/edit")
    public ResultEntity save(@RequestBody SysUserDto sysUserDto) {
        SysUser sysUser = new SysUser();
        BeanUtils.copyProperties(sysUserDto, sysUser);
        if (sysUserDto.getUserId() != null) {
            iSysUserService.update(sysUser);
        } else {
            iSysUserService.saveSysUser(sysUser);
        }
        return ResultEntity.ok("200", "保存成功");
    }

    /**
     * 修改用户
     */
    @RequestMapping("/getUserById")
    public ResultEntity getUserById(Long userId) {
        HashMap<String, Object> map = new HashMap<>();
        SysUser sysUser = iSysUserService.getUserById(userId);
        if (StringUtils.isBlank(sysUser.getPassword())) {
            sysUser.setPassword(null);
        } else {
            sysUser.setPassword(new Sha256Hash(sysUser.getPassword(), sysUser.getSalt()).toHex());
        }
        map.put("sysuser", sysUser);
        List<Long> userRoles = iSysUserRoleService.queryUserIdList(userId);
        map.put("userRoles", userRoles);
        if (sysUser.getDeptId() != null) {
            List<SysRole> deptRoles = iSysRoleDeptService.queryListByDeptId(sysUser.getDeptId());
            map.put("deptRoles", deptRoles);
        }
        List<Long> businessCircleIdList = iTbConfigUserBusinessCircleService.queryBusinessCircleIdList(userId);
        if (businessCircleIdList != null) {
            map.put("businessCircleIdList", businessCircleIdList);
        }
        if (sysUser.getCountry() != null) {
            List<ITbBusinessCircleService> businessCircle = iTbBusinessCircleService.queryList(sysUser.getCountry());
            map.put("businessCircle", businessCircle);
        }
        return ResultEntity.ok("200", "查询成功", map);
    }


    /**
     * 修改用户
     */
    @SysLog("修改用户")
    @RequestMapping("/update")
    public ResultEntity update(@RequestBody SysUserDto sysUserDto) {
        SysUser sysUser = new SysUser();
        BeanUtils.copyProperties(sysUserDto, sysUser);

        iSysUserService.update(sysUser);

        return ResultEntity.ok();
    }

    /**
     * 删除用户
     */
    @SysLog("删除用户")
    @RequestMapping("/delete")
    public ResultEntity delete(Long[] userIds) {
        try {
            iSysUserService.deleteBatch(userIds);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
        }
        return ResultEntity.ok();
    }

    @RequestMapping("/getBusinessIdByUserName")
    public SysUserDto getBusinessIdByUserId(@RequestBody String username){
        SysUserDto sysUser = iSysUserService.getBusinessIdByUserId(username);
        return sysUser;
    }
}

